using System;
using System.Collections.Generic;
using System.Text;
using DSP.MathLibrary;
using System.Linq;

namespace DSP.Library
{
    public class FourierTransform
    {
        public static List<ComplexNumber> CountFastFourierTransform(List<ComplexNumber> arr, bool inverse)
        {

            alglib.complex[] arrc = arr.Select(s => new alglib.complex(s.Re, s.Im)).ToArray();

            alglib.fftc1d(ref arrc);

            return arrc.Select(s => new ComplexNumber(s.x, s.y)).ToList();
            //List<ComplexNumber> lst = new List<ComplexNumber>();
            //int div = 1;
            //int sign = -1;

            //int N = arr.Count;

            //if (inverse)
            //{
            //    div = N;
            //    sign = 1;
            //}

            //for (int i = 0; i < N; i++)
            //{
            //    ComplexNumber tmp = new ComplexNumber(0, 0);
            //    for (int j = 0; j < N; j++)
            //    {
            //        ComplexNumber curr = arr[j];
            //        tmp += curr * ComplexNumber.Exp(new ComplexNumber(0, sign * 2 * Math.PI * j * i / N));
            //    }
            //    lst.Add(new ComplexNumber(tmp.Re / div, tmp.Im / div));
            //}

            //return lst;
        }

        public static List<ComplexNumber> CountSPM(List<ComplexNumber> arr)//, bool inverse)
        {

            alglib.complex[] arrc = arr.Select(s => new alglib.complex(s.Re, s.Im)).ToArray();

            alglib.fftc1d(ref arrc);

            return arrc.Select(s => (new ComplexNumber(s.x, s.y)) * (new ComplexNumber(s.x, -1.0 * s.y))).ToList();
            //List<ComplexNumber> lst = new List<ComplexNumber>();
            //int div = 1;
            //int sign = -1;

            //int N = arr.Count;

            //if (inverse)
            //{
            //    div = N;
            //    sign = 1;
            //}

            //for (int i = 0; i < N; i++)
            //{
            //    ComplexNumber tmp = new ComplexNumber(0, 0);
            //    for (int j = 0; j < N; j++)
            //    {
            //        ComplexNumber curr = arr[j];
            //        tmp += curr * ComplexNumber.Exp(new ComplexNumber(0, sign * 2 * Math.PI * j * i / N));
            //    }
            //    lst.Add(new ComplexNumber(tmp.Re / div, tmp.Im / div));
            //}

            //return lst;
        }

    }
}
